﻿using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Text;
using umbraco.cms.businesslogic.media;
using System.Web.UI.WebControls;

namespace Designit.Umbraco.Gallery.usercontrols.Designit.Gallery
{
    public partial class Gallery : System.Web.UI.UserControl
    {
        /// <summary>
        /// Gets or sets the media folder id.
        /// </summary>
        /// <value>The folder id.</value>
        public int folderId { get; set; }

        /// <summary>
        /// Gets or sets the width of the thumb.
        /// </summary>
        /// <value>The width of the thumb.</value>
        public int ThumbWidth { get; set; }
        /// <summary>
        /// Gets or sets the height of the thumb.
        /// </summary>
        /// <value>The height of the thumb.</value>
        public int ThumbHeight { get; set; }
        /// <summary>
        /// Gets or sets the width of the image in the lightbox.
        /// </summary>
        /// <value>The width of the image in the lightbox.</value>
        public int LightBoxWidth { get; set; }
        /// <summary>
        /// Gets or sets the height of the image in the lightbox.
        /// </summary>
        /// <value>The height of the image in the lightbox.</value>
        public int LightBoxHeight { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(folderId == 0)
                return;

            umbraco.library.AddJquery();

            if (!Page.ClientScript.IsClientScriptIncludeRegistered("jquery.lightbox-05.pack.js"))
                Page.ClientScript.RegisterClientScriptInclude("jquery.lightbox-05.pack.js", "/scripts/Designit.Gallery/jquery.lightbox-05.pack.js");

            if (!Page.ClientScript.IsClientScriptIncludeRegistered("jquery.lightbox-0.5.min.js"))
                Page.ClientScript.RegisterClientScriptInclude("jquery.lightbox-0.5.min.js", "/scripts/Designit.Gallery/jquery.lightbox-0.5.js");

            var head = Page.Header;
            var link = new HtmlLink();
            link.Attributes.Add("href", "/css/Designit.Gallery/jquery.lightbox-0.5.css");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            head.Controls.Add(link);


            if(!Page.ClientScript.IsStartupScriptRegistered("galleryInit"))
            {
                var sb = new StringBuilder();
                sb.AppendLine("<script type=\"text/javascript\">");
                sb.AppendLine("$(function() {");
                sb.AppendLine("  $('#diGallery a').lightBox();");
                sb.AppendLine("});");
                sb.AppendLine("</script>");
                Page.ClientScript.RegisterStartupScript(GetType(), "galleryInit", sb.ToString());
            }

            //var images = new List<ImageObject>();
            var folder = new Media(folderId);
            //foreach(Media media in folder.Children)
            //{
            //    var objImage = new ImageObject(media, ThumbWidth, ThumbHeight, LightBoxWidth, LightBoxHeight);
            //    images.Add(objImage);
            //}
            repImages.ItemDataBound += repImages_ItemDataBound;
            repImages.DataSource = folder.Children;
            repImages.DataBind();
        }

        void repImages_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;

            var currentMedia = (Media) e.Item.DataItem;
            var hlImage = (HyperLink) e.Item.FindControl("hlImage");
            var img = (Image) e.Item.FindControl("img");
            string path = currentMedia.getProperty("umbracoFile").Value.ToString();
            hlImage.NavigateUrl = GetImageUrl(path, false);
            hlImage.ToolTip = currentMedia.Text;
            img.ImageUrl = GetImageUrl(path, true);
            img.AlternateText = currentMedia.Text;
        }

        private string GetImageUrl(string imagePath, bool thumb)
        {
            int width = LightBoxWidth;
            int height = LightBoxHeight;
            if(thumb)
            {
                width = ThumbWidth;
                height = ThumbHeight;
            }
            return string.Format("/umbraco/ImageGen.ashx?image={0}&width={1}&height={2}&constrain=true",
                                               imagePath, width, height);
        }
    }
}